package com.hnust.root.web.controller.forward.user;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hnust.root.common.base.exception.BusinessException;
import com.hnust.root.common.base.response.BaseRestController;
import com.hnust.root.common.base.response.BaseResult;
import com.hnust.root.service.forword.wx.WxAccountService;
import com.hnust.root.web.shiro.JWTToken;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/user")
public class WxAppletController extends BaseRestController {
    @Autowired
    private WxAccountService wxAccountService;

    /**
     * 微信小程序登录
     *
     * @return
     */
    @ApiOperation("微信小程序用户通过code获取系统token")
    @PostMapping("/login")
    public BaseResult<JWTToken> wxAppletLogin(@RequestBody JSONObject jsonObject) throws BusinessException {
        String code = jsonObject.getStr("code");
        if (!StrUtil.isEmpty(code)) {
            return fillSuccessData(wxAccountService.wxLogin(code));
        }
        return fillFailData();
    }

    @ApiOperation("通过经纬度获取用户地理位置信息")
    @PostMapping("/getLocation")
    public BaseResult<String> getLocation(@RequestBody JSONObject jsonObject) throws BusinessException {
        String openId = getWxAccountOpenId();
        String location = jsonObject.getStr("location"); //经度 + 维度
        String city = wxAccountService.getLocation(location, openId);
        return fillSuccessData(city);
    }

}
